package com.kay.web;

import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

/**
 * @Description:
 *  完成AppConfig的注册，
 *  并基于java代码的方式初始化DispatcherServlet
 *  注意：
 *  Servlet 3.0+容器启动时将自动扫描类路径以查找实现Spring的 Webapplicationinitializer 接口的所有实现，
 *  将其放进一个Set集合中，提供给ServletContainerInitializer中onStartup方法的第一个参数。
 * @Author: Kay
 * @CreateDate: 2021/7/26$ 20:59$
 * @UpdateUser: Kay
 * @UpdateDate: 2021/7/26$ 20:59$
 */
public class MyWebApplicationInitializer extends MyApplicationContext implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) {
        System.out.println(" MyWebApplicationInitializer ............ ");
        // 将DispatcherServlet添加到tomcat上下文中去
        ServletRegistration.Dynamic registration = servletContext.addServlet("/", super.getDispatcherServlet());
        if (registration != null) {
            registration.setLoadOnStartup(1);
            registration.addMapping("/*");
        }
    }
}
